﻿using System.ComponentModel;

namespace System.Lua.Converters
{
    internal class LuaTypeConverter : ILuaTypeConverter
    {
        private readonly TypeConverter typeConverter;

        /// <summary>
        /// Initializes a new instance of the <see cref="LuaTypeConverter"/> class.
        /// </summary>
        /// <param name="typeConverter">The type converter.</param>
        public LuaTypeConverter(TypeConverter typeConverter)
        {
            if (typeConverter == null)
                throw new ArgumentNullException("typeConverter");

            this.typeConverter = typeConverter;
        }

        public string ConvertToString(object value)
        {
            return (string)this.ConvertTo(value, typeof(string));
        }

        public object ConvertFromString(string value)
        {
            return this.ConvertFrom(value);
        }

        public virtual bool CanConvertFrom(Type type)
        {
            return typeConverter.CanConvertFrom(type);
        }

        public virtual bool CanConvertTo(Type targetType)
        {
            return typeConverter.CanConvertTo(targetType);
        }

        public virtual object ConvertFrom(object value)
        {
            return typeConverter.ConvertFrom(value);
        }

        public virtual object ConvertTo(object value, Type targetType)
        {
            return typeConverter.ConvertTo(value, targetType);
        }

        /// <summary>
        /// Gets a value indicating whether the string returned by the convert is a valid Lua
        /// literal.
        /// </summary>
        public virtual bool ReturnsLiteral
        {
            get { return false; }
        }
    }

    internal class LuaTypeConverter<TTypeConverter> : LuaTypeConverter where TTypeConverter : TypeConverter, new()
    {
        protected LuaTypeConverter()
            : base(new TTypeConverter())
        {
        }
    }
}